home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / BOTKILL.sit / BOTKILL / BOTKILL / Source / files.c < prev    next >
C/C++ Source or Header  |  1993-06-15  |  3KB  |  108 lines

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include "config.h"
  5.  
  6. /*
  7.   PROCEDURE read_file( list, filename )
  8.   INPUTS:
  9.      list        a pointer to a list structure
  10.      filename    a pointer to a string
  11.   REQUIRES:
  12.      each line in the file consists of a string followed by a number.
  13.      The string and number may be separated by any number of spaces or
  14.      tabs.
  15.   MODIFIES:
  16.      list
  17.   EFFECTS:
  18.      Reads a list of file records from the file.
  19.      Stores the entries in the list in sequential order.
  20.      Modifies list->num_entries so that it is the number of entries in the
  21.      list.  If there are more lines of text in the file than MAX_F_LINES,
  22.      the procedure ignores the extra lines.
  23. */
  24.  
  25. void read_file( list, filename )
  26.      struct list_rec *list;
  27.      char *filename;
  28. {
  29.   int i = 0;
  30.   FILE *the_file;
  31.   char line[BUFLEN];
  32.   char *token;
  33.  
  34.   /* Open the file */
  35.   if ( DEBUG )
  36.     printf( "PROC read_file: attempting to open file %s.\n", filename );
  37.   if ( ( the_file = fopen( filename, "r" ) ) == NULL )
  38.     {
  39.       printf( "Couldn't open file %s.\n", filename );
  40.       exit ( 1 );
  41.     }
  42.   if ( DEBUG )
  43.     printf( "PROC read_file: successfully opened file %s.\n", filename );
  44.   
  45.   /* Read the lines of the file into the list */
  46.   while ( ( fgets( line, BUFLEN, the_file ) != NULL ) &&
  47.       ( i < MAX_F_LINES ) )
  48.     {
  49.       if ( ( token = strtok( line, " \t\n" ) ) != NULL )
  50.     {
  51.       strcpy( ( list->entry )[i].text, token );
  52.       ( list->entry )[i].num = atoi( strtok( NULL, " \t\n" ) );
  53.       i++;
  54.     }
  55.     }
  56.  
  57.   fclose( the_file );
  58.   list->num_entries = i;
  59. }
  60.  
  61.  
  62. /*
  63.   PROCEDURE write_file( list, filename )
  64.   INPUTS:
  65.      list       a pointer to a list structure
  66.      filename   a pointer to a string
  67.   EFFECTS:
  68.      writes the list to the file.  The format of each line in the file is
  69.      the text, which is followed by three tabs and then the number.
  70. */
  71.  
  72. void write_file( list, filename )
  73.      struct list_rec *list;
  74.      char *filename;
  75. {
  76.   int i = 0;
  77.   FILE *the_file;
  78.   char line[BUFLEN];
  79.  
  80.   /* Open the file */
  81.   if ( DEBUG )
  82.     printf( "PROC write_file: attempting to open file %s.\n", filename );
  83.   if ( ( the_file = fopen( filename, "w" ) ) == NULL )
  84.     {
  85.       printf( "Couldn't open file %s.\n", filename );
  86.       exit ( 1 );
  87.     }
  88.  
  89.   /* Write the list into the file */
  90.   while ( i < ( list->num_entries ) )
  91.     {
  92.       sprintf( line, "%s\t\t\t%d\n", ( list->entry )[i].text,
  93.            ( list->entry )[i].num );
  94.       if ( fputs( line, the_file ) == NULL )
  95.     {
  96.       printf( "Encountered difficulty writing to file %s\n.", filename );
  97.       exit( 1 );
  98.     }
  99.     }
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.